<?php include('scripts.php'); ?>
<script type="text/javascript">
var packageStrings = "<option value=''></option>";
var datagrid = null;
var datagridAnnouncement = null;
$(function() {
    datagrid = $("#datagridAnnouncement").r9jasonDataTable({
        // online1DataTable extra params
        "idTr" : true, // assign <tr id='xxx'> from 1st columns array(aoColumns);
        "extraParam" : function(aoData){ // pass extra params to server

        },
        "reassignEvent" : function(){ // extra function for reassignEvent when JSON is back from server
            reassignDatagridAnnouncementEventAttr();
        },
        // datatables params
        "bLengthChange": true,
        "bFilter": false,
        "bProcessing": true,
        "bServerSide": true,
        "bAutoWidth": false,
        "sAjaxSource": "/member/announcementList",
        "sPaginationType": "full_numbers",
        "aaSorting": [[4,'desc']],
        "aoColumns": [
                      { "sName" : "announcement_id",  "bVisible": false},
                        { "sName" : "title",  "bSortable": false, "fnRender": function ( oObj ) {
                            return "<a class='announcementLink' refId='" + oObj.aData[0] + "' href='#' style='color: #0000ff;'>" + oObj.aData[1] + "</a>";
		  		        }},
                      { "sName" : "created_on",  "bSortable": false}
        ]
    });

    $(".deleteLink").button({
        icons: {
            primary: "ui-icon-circle-close"
        }
    });
    $(".activeLink").button({
        icons: {
            primary: "ui-icon-circle-check"
        }
    }).click(function(event){
        event.preventDefault();
        waiting();
        $("#distributorId").val($(this).attr("ref"));
        $("#dgActivateMember_shareholderId").val($(this).attr("refCode"));
        $("#dgActivateMember_alias").val($(this).attr("refNickname"));
        $("#dgActivateMember_registeredTime").val($(this).attr("refCreatedDate"));
        $.ajax({
            type : 'POST',
            url : "/member/fetchPackage",
            dataType : 'json',
            cache: false,
            data: {
            },
            success : function(data) {
                $.unblockUI();
                packageStrings = "";
                jQuery.each(data.package, function(key, value) {
                    packageStrings += "<option value='" + value.price + "' ref='" + value.packageId + "'>" + value.name + "</option>";
                });

                $("#dgActivateMember").dialog("open");

                $("#dgActivateMember_point").html(packageStrings).trigger("change");

                $("#dgActivateMember_pointAvail").val(data.point);
                $("#dgActivateMember_ecash").val(data.ecash);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("Server connection error.");
            }
        });
    });

    $("#dgActivateMember").dialog("destroy");
    $("#dgActivateMember").dialog({
        autoOpen : false,
        modal : true,
        resizable : false,
        hide: 'clip',
        show: 'slide',
        width: 400,
        buttons: {
            "<?php echo __('Cancel') ?>": function() {
                $(this).dialog('close');
            },
            "<?php echo __('Submit') ?>": function() {
                if ($("#dgActivateMember_point").val() == 0 || $("#dgActivateMember_point").val() == "") {
                    alert("e-Point cannot be zero.");
                    $("#dgActivateMember_point").focus().select();
                } else if ($("#paymentTypeEPoint").is(':checked') == true && parseFloat($("#dgActivateMember_point").val()) > parseFloat($("#dgActivateMember_pointAvail").val())){
                    alert("In-sufficient e-point. ");
//                    alert("In-sufficient e-point. " + $("#dgActivateMember_pointAvail").val());
                    $("#dgActivateMember_point").focus().select();
                } else if ($("#paymentTypeECash").is(':checked') == true && parseFloat($("#dgActivateMember_point").val()) > parseFloat($("#dgActivateMember_ecash").val())){
                    alert("In-sufficient e-cash. ");
//                    alert("In-sufficient e-cash. " + $("#dgActivateMember_ecash").val());
                    $("#dgActivateMember_point").focus().select();
                } else {
                    if ($.trim($("#dgActivateMember_transactionPassword").val()) == "") {
                        alert("Security Password is empty");
                        $("#dgActivateMember_transactionPassword").focus();
                        return false;
                    }
                    waiting();
                    $.ajax({
                        type : 'POST',
                        url : "/member/activateMember",
                        dataType : 'json',
                        cache: false,
                        data: {
                            packageId : $('#dgActivateMember_point option:selected').attr("ref")
                            , transactionPassword : $('#dgActivateMember_transactionPassword').val()
                            , sponsorId : $('#distributorId').val()
                            , paymentType : $("input[name='paymentType']:checked").val()
                        },
                        success : function(data) {
                            if (data.error == false) {
                                $.unblockUI();
                                var sure = confirm("<?php echo __('Member activated successfully.').'\n'.__('Do you want to proceed to member placement?') ?>");
                                if (sure) {
                                    window.location = "<?php echo url_for('/member/placementTree') ?>";
                                } else {
                                    window.location = "<?php echo url_for('/member/summary') ?>";
                                }
                            } else {
                                alert(data.errorMsg);
                                $("#dgActivateMember_transactionPassword").focus().select();
                            }
                        },
                        error : function(XMLHttpRequest, textStatus, errorThrown) {
                            alert("Server connection error.");
                        }
                    });
                }
            }
        },
        open: function() {

        },
        close: function() {

        }
    });

    $("#dgActivateMember_point").change(function(){
        $("#dgActivateMember_pointNeeded").val($("#dgActivateMember_point").val());
    });

    $("#dgAnnouncement").dialog("destroy");
    $("#dgAnnouncement").dialog({
        autoOpen : false,
        modal : true,
        resizable : false,
        hide: 'clip',
        show: 'slide',
        width: 500,
        height: 250,
        buttons: {
            "<?php echo __('Close') ?>": function() {
                $(this).dialog('close');
            }
        },
        open: function() {

        },
        close: function() {

        }
    });

    $("#reinvestGapLink").live("click", function(event) {
        event.preventDefault();
        $("#dgReinvestCps").dialog("open");
    });
    $(".announcementLink").live("click", function(event) {
        event.preventDefault();
        $("#dgAnnouncement").data("refId", $(this).attr("refId"));

        waiting();
        $.ajax({
            type : 'POST',
            url : "/member/fetchAnnouncementById",
            dataType : 'json',
            cache: false,
            data: {
                announcementId : $('#dgAnnouncement').data("refId")
            },
            success : function(data) {
                $.unblockUI();
                $("#dgAnnouncement").dialog("open");
                $("#tdAnnouncement").html("<strong>" + data.content + "</strong>");
                $("#dgAnnouncement").dialog("option", "title", data.title);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("Your login attempt was not successful. Please try again.");
            }
        });
    });
    $("#paymentTypeEPoint").attr('checked', true);
    $("#spanPaymentType").buttonset();

    $.ajax({
        type : 'POST',
        url : "/member/fetchAnnouncementById",
        dataType : 'json',
        cache: false,
        data: {
            announcementId : 1
        },
        success : function(data) {
            $.unblockUI();
            $("#dgAnnouncement").dialog("open");
            $("#tdAnnouncement").html("<strong>" + data.content + "</strong>");
            $("#dgAnnouncement").dialog("option", "title", data.title);
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert("Your login attempt was not successful. Please try again.");
        }
    });
});
function reassignDatagridAnnouncementEventAttr(){
	$("a[id=editLink]").click(function(event){

	});
}
</script>

<form action="/member/activateMember" method="post" id="memberForm">
<input type="hidden" id="distributorId">
<div style="padding: 10px; top: 30px; width: 98%">
<h2><?php echo __('Summary'); ?></h2>
<br>

<div class="portlet">
    <div class="portlet-header"><?php echo __('Inactive Shareholders') ?></div>
    <div class="portlet-content">

        <table class="display" id="datagrid" border="0" width="100%">
            <thead>
            <tr>
				<th><?php echo __('Shareholder ID') ?></th>
                <th><?php echo __('Alias') ?></th>
                <th><?php echo __('Registered Time') ?></th>
                <th><?php echo __('Action') ?></th>
			</tr>
            </thead>
            <tbody>
			<?php
            if (count($TblDistributor) > 0) {
                $trStyle = "even";
                foreach($TblDistributor as $dist){
                    if ($trStyle == "even") {
                        $trStyle ="odd";
                    } else {
                        $trStyle ="even";
                    }

                    echo "<tr class='".$trStyle."'>
				        <td align='center'>".$dist->getDistributorCode()."</td><td align='center'>".$dist->getNickname()."</td>
				        <td align='center'>".$dist->getCreatedOn()."</td>
				        <td align='center'>".link_to(__('Active'), '#', array (
                                                                              'class' => 'activeLink',
                                                                              'ref' => $dist->getDistributorId(),
                                                                              'refCode' => $dist->getDistributorCode(),
                                                                              'refNickname' => $dist->getNickname(),
                                                                              'refCreatedDate' => $dist->getCreatedOn(),
                                                                        ))."&nbsp;".link_to(__('Delete'), 'member/delete?distid='.$dist->getDistributorId(), array (
                                                                                                                                                                   'class' => 'deleteLink',
                                                                                                                                                                   'confirm=Are you sure you want to remove?'
                                                                                                                                                             ))."</td></tr>";
                }
            } else {
                echo "<tr class='odd' align='center'><td colspan='4'>". __('No records found')."</td></tr>";
            }
			?>
            </tbody>
        </table>
    </div>
</div>

<div class="portlet">
    <div class="portlet-header"><?php echo __('Announcements') ?></div>
    <div class="portlet-content">
            <table class="display" id="datagridAnnouncement" border="0" width="100%">
                <thead>
                <tr>
                    <th>Announcement Id[hidden]</th>
                    <th><?php echo __('Title') ?></th>
                    <th width="20%"><?php echo __('Date') ?></th>
                </tr>
                </thead>
            </table>
    </div>
</div>
</div>
</form>

<div id="dgActivateMember" title="<?php echo __('Activate Shareholder') ?>" style="display:none;">
    <input type="hidden" id="dgActivateMember_ecash">
    <input type="hidden" id="dgActivateMember_pointAvail"/>
    <table cellspacing="5" cellpadding="3">
        <tr>
            <td class="text" width="30%"><label><?php echo __('Shareholder ID') ?></label></td>
            <td>:</td>
            <td><input type="text" disabled="disabled" id="dgActivateMember_shareholderId" class="text ui-widget-content ui-corner-all"/></td>
        </tr>
        <tr>
            <td class="text"><label><?php echo __('Alias') ?></label></td>
            <td>:</td>
            <td><input type="text" disabled="disabled" id="dgActivateMember_alias" class="text ui-widget-content ui-corner-all"/></td>
        </tr>
        <tr>
            <td class="text"><label><?php echo __('Registered Time') ?></label></td>
            <td>:</td>
            <td><input type="text" disabled="disabled" id="dgActivateMember_registeredTime" class="text ui-widget-content ui-corner-all"/></td>
        </tr>
        <tr>
            <td class="text"><label><?php echo __('Package Type') ?></label></td>
            <td>:</td>
            <td>
                <select name="dgActivateMember_point" id="dgActivateMember_point" class='text ui-widget-content ui-corner-all'>

                </select>
                <input type="text" disabled="disabled" id="dgActivateMember_pointNeeded" class="text ui-widget-content ui-corner-all" size="10px"/>
            </td>
        </tr>
        <tr>
            <td class="text"><label><?php echo __('Payment Type') ?></label></td>
            <td>:</td>
            <td>
                <span id="spanPaymentType">
                    <input type="radio" id="paymentTypeEPoint" name="paymentType" value="epoint"/><label for="paymentTypeEPoint"><?php echo __('e-Point') ?></label>
                    <input type="radio" id="paymentTypeECash" name="paymentType" value="ecash"/><label for="paymentTypeECash"><?php echo __('e-Cash') ?></label>
                </span>
            </td>
        </tr>
        <tr>
            <td class="text"><label><?php echo __('Security Password') ?></label></td>
            <td>:</td>
            <td><input type="password" id="dgActivateMember_transactionPassword" class="text ui-widget-content ui-corner-all"/></td>
        </tr>
    </table>
</div>
<div id="dgAnnouncement" title="<?php echo __('Announcements') ?>" style="display:none;">
    <table cellspacing="5">
        <tr>
            <td class="text" id="tdAnnouncement"></td>
    </table>
</div>